﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Billing.DataObjects;

namespace Billing.Windows.Shell
{
    public partial class LogInForm : System.Windows.Forms.Form
    {
        public LogInForm()
        {
            InitializeComponent();
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            try
            {
                this.txtUserName.Appearance.ResetBackColor();
                this.txtPassword.Appearance.ResetBackColor();
                if (this.txtUserName.Text.Trim() == string.Empty)
                {
                    this.txtUserName.Appearance.BackColor = Color.Red;
                    this.txtUserName.Focus();
                    return;
                }

                this.Cursor = Cursors.WaitCursor;

                try
                {
                    using (ShellDataContext shellDataContext = new ShellDataContext())
                    {
                        int? userID = 0, departmentID = 0, result = 0;
                        Guid? sessionGUID = Guid.Empty, userGUID = Guid.Empty;
                        string userName = String.Empty;

                        shellDataContext.UserLogOn(this.txtUserName.Text, new CryptoUtils().GetMD5(this.txtPassword.Text), (int?)0, (int?)0, (int?)0, (int?)0, ref userID, ref departmentID, ref sessionGUID, ref userGUID, ref userName, ref result);
                        if (result != 0)
                            throw new Billing.UserException((Billing.UserException.eLogInError)result);

                        if (this.txtPassword.Text == String.Empty)
                            Program.IsFirstLogin = true;

                        Profile.UserGUID = userGUID;
                        Profile.UserID = userID;
                        Profile.UserName = userName;
                        Profile.DepartmentID = departmentID;
                        Profile.SessionGUID = sessionGUID;
                    }
                    Program.IsAuthorized = true;

                }
                catch (UserException ex)
                {
                    switch (ex.LogInError)
                    {
                        case UserException.eLogInError.InvalidLastConnection:
                            break;
                        case UserException.eLogInError.InvalidPWD:
                            this.txtPassword.Appearance.BackColor = Color.Red;
                            this.txtPassword.Focus();
                            break;
                        case UserException.eLogInError.InvalidUserName:
                            this.txtUserName.Appearance.BackColor = Color.Red;
                            this.txtUserName.Focus();
                            break;
                        case UserException.eLogInError.AlredyAuthorized:
                            throw new Exception(ex.Message);
                        case UserException.eLogInError.ProgWorkLocalModeinvalidpwd:
                            throw new Exception(ex.Message);
                        case UserException.eLogInError.ProgWorkLocalModeinvalidUser:
                            throw new Exception(ex.Message);
                        case UserException.eLogInError.UnknowError:
                            throw new Exception(ex.Message);
                        default:
                            break;
                    }
                    return;
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
            catch (Exception ex)
            {
                string str = "";

                if (ex.InnerException != null && ex.InnerException.Message != null)
                    str = ex.InnerException.Message;

                str += ex.Message;

                MessageBox.Show(str);
            }

            finally
            {
                this.Cursor = Cursors.Default;
            }

        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Program.IsAuthorized = false;
            this.Close();
        }

        private void frmLogIn_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.DialogResult == DialogResult.OK)
                global::Billing.Windows.Shell.Properties.Settings.Default.Save();
        }

        private void LogInForm_Load(object sender, EventArgs e)
        {
            if (txtUserName.Text != String.Empty)
                txtPassword.Select();
//#if DEBUG
//            txtUserName.Text = "rb";
//            txtPassword.Text = "1";
//            btnOk_Click(btnOk, EventArgs.Empty);
//#endif
        }

    }
}
